home *** CD-ROM | disk | FTP | other *** search
- Path: mayne.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Is This Bad Coding Practice?
- Date: 29 Mar 1996 10:25:32 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4jh9usINNr7p@mayne.ugrad.cs.ubc.ca>
- References: <4jgnt2$9d1@loki.tor.hookup.net>
- NNTP-Posting-Host: mayne.ugrad.cs.ubc.ca
-
- In article <4jgnt2$9d1@loki.tor.hookup.net>,
- Rajendra Singh <Rajendra_Singh@msn.com> wrote:
- >int main(int argc, char *argv[])
- > {
- > char string[128];
- > char *func1(void);
- >
- > strcpy(string, func1());
- > return 0;
- > }
- >
- >char *func1(void)
- > {
- > char test[100];
- >
- > sprintf(test, "Test: %d", 1);
- > return test;
- > }
- >
- >... since I am using the value returned from func1() immediately (in
- >main()), is this reliable? After I copy it into "string", I won't be
- >using that area of memory anymore (i. e. the pointer returned by
- >func1()).
-
- Absolutely not. Either malloc the space, or make it static, or allow the caller
- to specify storage for the characters:
-
- void func1(char *bufferspace, int size);
-
- Otherwise you are relying on the idea that stack frames are being used in a
- certain way. It's possible for a C implementation to not use stack frames to
- represent activation records. It's even possible that the pointer to test is
- completely bound to the activation record, so that it has no de facto meaning
- outside of the function.
-
- As far as standard C is concerned, the test[] object no longer exists once the
- function terminates, and the pointer that was once bound to it is no longer
- valid.
- --
-
-